<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\AdminController;
use app\admin\model\Admin;
use app\admin\model\Type;
use app\admin\model\City;
use app\Request;
use think\exception\ValidateException;
class Index extends AdminController
{
    public function index()
    {
        return '您好！这是一个[admin]示例应用';
    }
    public function show(){
        return view('show');
    }
    public function welcome(){
        return view('welcome');
    }

    public function list(){
        $data=Admin::with(['province','City','area'])->order('id','desc')->paginate(3);
//        dd($data->toArray());
        return view('list',compact('data'));
    }
        public function huis(){
            $data=Admin::with(['province','City','area'])->onlyTrashed()->order('id','desc')->paginate(3);
    //        dd($data);
            return view('huis',['data'=>$data]);
        }
    public function add(Request $request){


        $error='';
        $citvs=City::where('pid',0)->select();




//        dd($citys);
        if($request->isPost()){
            $data=$request->post();
//            dd($data);
            try {
                $file = $request->file('img');
                $data['img']=$file;
//                dd($data);
                validate(\app\admin\validate\Admin::class)->check($data);
                $data['img'] = \think\facade\Filesystem::disk('public')->putFile( 'topic', $file);
//               dd($data);

                $path=public_path().'storage/'.$data['img'];
                $image = \think\Image::open($path);
//                dd($path);
                $pathAll=pathinfo($path);
//                dd($pathAll);
                $pathX=$pathAll['dirname'].'/'.$pathAll['filename'].'_100.'.$pathAll['extension'];
//dd($pathX);
// 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png
                $image->thumb(150,150,\think\Image::THUMB_CENTER)


                    ->save($pathX);

                $res=Admin::create($data);
                if($res){
                    return redirect(url('index/list')->build());
                }else{
                    return '添加失败';
                }
            } catch (ValidateException $e) {
                // 验证失败 输出错误信息
                $error=$e->getError();
            } catch (\Exception $e){
                $error=$e->getMessage();
            }
        }
        $type=Type::select();


        return view('',compact('error','type','citvs'));


    }
    public function getChilc(Request $request){
        $pid=$request->post('pid');
//        dd($pid);
        $citvs=City::where('pid',$pid)->select();
//        dd($citvs);
        return $citvs;
    }
    public function delete(Request $request){
        $data=$request->post();
        $res=Admin::destroy($data);
        if($res){
            return ['status'=>1,'msg'=>'删除成功'];
        }else{
            return ['status'=>2,'msg'=>'删除失败'];
        }
    }
    public function huif(Request $request){
        $id=$request->post('id');
//        dd($id);
        $user = Admin::onlyTrashed()->find($id);
        $user->restore();
        return [
           'status'=>1,
            'msg'=>'恢复成功'
        ];
    }

    public function updei(Request $request){
        $id=$request->post();
        $res=Admin::find($id);
        if($res->stater==1){
            $res->stater=0;
            $html='隐藏';
            $msg='隐藏成功';
        }else{
            $res->stater=1;
            $html='显示';
            $msg='显示成功';
        }
        $res->save();
        if($res) {
            return json([
                'status' => 1,
                'msg' => $msg,
                'html' => $html,
            ]);

        }

    }
    /***
     *  退出登录
     */
    public function tuic(){
        session('adminIf',null);
        return redirect(url('login/login')->build());
    }

}
